var fly = {
    flyToViewPoint: function (position, direction, up) {
        //飞行到某一个点
        viewer.camera.flyTo({
            destination: new LSGlobe.Cartesian3(position.x, position.y, position.z),
            orientation: {
                direction: new LSGlobe.Cartesian3(direction.x, direction.y, direction.z),
                up: new LSGlobe.Cartesian3(up.x, up.y, up.z)
            }
        });
    },
    getFlyViewPos: function (xGuid) {
        //初始视角定位
        var datas = {};
        datas.guid = xGuid;
        var url = basePath + "/TX_SceneManager/vp/queryViewPointByGuid.action";
        $.ajax({
            url: url,
            type: "post",
            data: datas,
            async: false,
            dataType: "Json",
            success: function (result) {
                if (result.resultCode == "1") {
                    var oPosition = {};
                    oPosition.x = result.viewpoint.posx;
                    oPosition.y = result.viewpoint.posy;
                    oPosition.z = result.viewpoint.posz;

                    var oUp = {};
                    oUp.x = result.viewpoint.upx;
                    oUp.y = result.viewpoint.upy;
                    oUp.z = result.viewpoint.upz;

                    var oDirection = {};
                    oDirection.x = result.viewpoint.directionx;
                    oDirection.y = result.viewpoint.directiony;
                    oDirection.z = result.viewpoint.directionz;

                    fly.flyToViewPoint(oPosition, oDirection, oUp);
                } else {
                    console.error(result.resultMsg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('body').dailog({ type: 'danger', bottons: ['确定'], discription: "获取默认视角失败" });
            }
        });
    },
    getLinesPoint: function (CurrentLineId, type) {
        /*查询一条线  */
        var datas = "guid=" + CurrentLineId;
        var url = basePath + "/vp/getViewPointsByFlyLine.action";
        var lineData = "";
        $.ajax(
		        {
		            url: url,
		            type: "post",
		            data: datas,
		            async: false,
		            dataType: "Json",
		            success: function (result) {
		                if (result.resultCode == "1") {
		                    if (type == "fly") {
		                        CurrentLine = result.vplist;
		                    } else {
		                        lineData = result.vplist;
		                    }
		                } else {
		                    console.error(result.resultMsg);
		                }
		            },
		            error: function (XMLHttpRequest, textStatus, errorThrown) {
		                $('body').dailog({ type: 'danger', bottons: ['确定'], discription: "获取单条飞行路径失败" });
		            }
		        });
        return lineData;
    },
    loadFlylines: function () {
        //获取飞行路径
        var datas = "guid=" + dataGuid;
        var url = basePath + "/vp/getFlyLineByDataGuid.action";
        $.ajax({
            url: url,
            type: "post",
            data: datas,
            async: false,
            dataType: "Json",
            success: function (result) {
                if (result.resultCode == "1") {
                    var lines = result.list;
                    var lineNumber = lines.length;
                    var oUl = $(".flyBotttom .flySelect ul");
                    if (lineNumber == 0) {
                        $("#fly").remove();
                    } else {
                        for (var a = 0; a < lines.length; a++) {
                            var oLine = "<li id=\"" + lines[a].guid + "\">" + lines[a].name + "</li>";
                            oUl.prepend(oLine);
                            if (a == 0) {
                                oUl.prev().find('font').html(lines[a].name);
                                CurrentLineId = lines[a].guid;
                                fly.getLinesPoint(CurrentLineId, "fly")
                            }
                        }
                        oUl.attr("style", "height:" + (lineNumber * 20 + 3) + "px;top:-" + (lineNumber * 20) + "px;");
                    }
                } else {
                    console.error(result.resultMsg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('body').dailog({ type: 'danger', bottons: ['确定'], discription: "获取飞行路径失败" });
            }
        });
    },
    createLine: function (cameraTrackControls, keys, currentLineLength, type) {
        //飞行函数 
        var timeDelta = 0;
        for (var i = 0; i < keys.length; i++) {
            cameraTrackControls.getAllKeys().push({
                destination: new LSGlobe.Cartesian3(keys[i].posx, keys[i].posy, keys[i].posz),
                orientation: {
                    direction: new LSGlobe.Cartesian3(keys[i].directionx, keys[i].directiony, keys[i].directionz),
                    up: new LSGlobe.Cartesian3(keys[i].upx, keys[i].upy, keys[i].upz)
                },
                time: timeDelta
            });
            timeDelta += parseInt(keys[i].timeInterval) * 1000;
            if (type == "fly") {
                var oLeftlength = 9;
                var lengyh = (880 / currentLineLength) * (i + 1);
                oLeftlength += lengyh
                $(".flybottomTwo .progressFinger").animate({
                    left: oLeftlength + "px"
                }, parseInt(keys[i].timeInterval) * 1000);
                currentLineTime.push(parseInt(keys[i].timeInterval) * 1000);
            }
            if (i == 0) {
                cameraTrackControls.getAllKeys().push({
                    destination: new LSGlobe.Cartesian3(keys[i].posx, keys[i].posy, keys[i].posz),
                    orientation: {
                        direction: new LSGlobe.Cartesian3(keys[i].directionx, keys[i].directiony, keys[i].directionz),
                        up: new LSGlobe.Cartesian3(keys[i].upx, keys[i].upy, keys[i].upz)
                    },
                    time: timeDelta
                });
                timeDelta += parseInt(keys[i].timeInterval) * 1000;
            }
        }
        cameraTrackControls.play();
        iAllDelTime = timeDelta;
    },
    init: function () {
        //飞到默认视角
        fly.getFlyViewPos(dataGuid);

        /*打开或关闭飞行路径显示 */
        $("#fly").on("click", function () {
            disabledDantihua(false);

            var oThis = $(this);
            if (oThis.hasClass("active")) {
                oThis.removeClass("active");
                $("#flyBotttom,#flyPogress").hide();
                $(".flybottomTwo .progressFinger").stop(true).animate({ left: "9px" });
                cameraTrackControls.stop();
                cameraTrackControls.clear();
            } else {
                page.initialStatus("fly");
                $("#flyBotttom").show();
                oThis.parents("#switch-box").find(".bar-item").removeClass("active");
                oThis.addClass("active");
            }
        });

        //飞行到原视点
        $("#back-position").click(function () {
            //恢复初始状态
            var initPageStatus = $(this).attr("init-status");
            if (initPageStatus == 1)
                page.initialStatus("back-position");

            var oThis = $(this);
            oThis.parents("#switch-box").find(".bar-item").removeClass("active");
            if (!oThis.hasClass("noClick")) {
                //飞行到初始视觉定位
                fly.getFlyViewPos(dataGuid);
            }
        });


        cameraTrackControls = new LSGlobe.LSCameraTrackControls(viewer.scene);
        //加载飞行线路
        fly.loadFlylines();

        //打开飞行路径列表
        $(".flyBotttom .flySelect .menuUp").click(function () {
            var oThis = $(this);
            oThis.parent().next().show();
        });
        //选择飞行路径
        $(".flyBotttom").on("click", ".flySelect li", function (e) {
            var oThis = $(this);
            var flyline = oThis.attr("id");
            var oText = oThis.html();
            oThis.parent().prev().find("font").html(oText);
            oThis.parent().hide();
            CurrentLineId = flyline;
            getLinesPoint(CurrentLineId, "fly");
        });
        var timer;
        $("#flyPlay").on("click", function () {
            $("#flyPogress").show();
            $("#flyBotttom,#analysis-panel,#switch-box,.sceneVedio").hide();
            $("#flyPogress .progressBox").html("");
            $("#start").removeClass("active");
            $("#start").next().html("暂停");
            var currentLineLength = CurrentLine.length;
            var oPointProgress = "";
            for (var i = 1; i <= currentLineLength; i++) {
                if (i == 1) {
                    oPointProgress += '<div class="first" img-url=\"' + CurrentLine[i - 1].viewpointImagePath + '"\" id="' + CurrentLine[i - 1].guid + '" point-id="' + i + '"><p></p><i class="number">0</i><i class="text">视点</i></div>';
                } else if (i == currentLineLength) {
                    oPointProgress += '<div class="last" img-url=\"' + CurrentLine[i - 1].viewpointImagePath + '"\" id="' + CurrentLine[i - 1].guid + '" point-id="' + i + '"><p></p><i class="number">' + currentLineLength + '</i></div>';
                } else {
                    if (i % 5 == 0) {
                        if (i % 10 == 0) {
                            oPointProgress += '<div class="ten" img-url=\"' + CurrentLine[i - 1].viewpointImagePath + '"\" id="' + CurrentLine[i - 1].guid + '" point-id="' + i + '"><p ></p><i class="number">' + i + '</i></div>';
                        } else {
                            oPointProgress += '<div class="five" img-url=\"' + CurrentLine[i - 1].viewpointImagePath + '"\" id="' + CurrentLine[i - 1].guid + '" point-id="' + i + '"><p ></p><i class="number">' + i + '</i></div>';
                        }
                    } else {
                        oPointProgress += '<div img-url=\"' + CurrentLine[i - 1].viewpointImagePath + '"\" id="' + CurrentLine[i - 1].guid + '" point-id="' + i + '"><p></p></div>';
                    }
                }
            }
            $("#flyPogress .progressBox").append(oPointProgress);
            $(".flybottomTwo .progressBox>div").attr("style", "width:" + 880 / currentLineLength + "px;");
            cameraTrackControls.clear();
            //创建飞行线路
            fly.createLine(cameraTrackControls, CurrentLine, currentLineLength, "fly");
            timer = setInterval(function () {
                var oLeft = $(".flybottomTwo .progressFinger").css("left");
                var oleftLength = oLeft.substring(0, (oLeft.length - 2));
                if (oleftLength > 880) {
                    clearInterval(timer);
                    $(".flybottomTwo .progressFinger").animate({
                        left: "9px"
                    });
                    if ($(".EarthFoot>div.active").find("i").hasClass("vis")) {
                        $("#flyBotttom,#switch-box,#analysis-panel,.sceneVedio").show();
                    } else {
                        $("#flyBotttom,#switch-box,.sceneVedio").show();
                    }
                    $("#flyPogress").hide();
                }
            }, 1000);
        });
        //暂停 或继续  
        var startValid = false;
        $("#start").on("click", function () {
            var oThis = $(this);
            var currentLineLength = CurrentLine.length;
            if (oThis.hasClass("active")) {
                cameraTrackControls.play();
                oThis.removeClass("active");
                oThis.next().html("暂停");
                var oLeft = $(".flybottomTwo .progressFinger").css("left");
                var oleftLength = oLeft.substring(0, (oLeft.length - 2));
                var oCurrentTime = (oleftLength - 9) / (880 / currentLineLength);
                //当前第几个
                var oCurrentNumber = Math.ceil(oCurrentTime);
                var oCurrent = (Math.ceil(oCurrentTime) - oCurrentTime) * currentLineTime[oCurrentNumber - 1];
                addFingerMove(Math.ceil(oCurrentTime), oCurrent);
                startValid = true;
            } else {
                oThis.addClass("active");
                oThis.next().html("继续 ");
                cameraTrackControls.pause();
                $(".flybottomTwo .progressFinger").stop(true);
                startValid = false;
            }
        });
        function addFingerMove(PointNumber, firstAnimate) {
            var currentLineLength = CurrentLine.length;
            var firstLength = (880 / currentLineLength) * PointNumber + 9;
            $(".flybottomTwo .progressFinger").animate({ left: firstLength + "px" }, firstAnimate);
            var oPointNumber = PointNumber + 1;
            if (oPointNumber <= currentLineLength && !startValid) {
                addFingerMove(oPointNumber, currentLineTime[oPointNumber - 1]);
            }
        }
        //停止
        $("#stop").on("click", function () {
            $(".flybottomTwo .progressFinger").stop(true).animate({ left: "9px" });
            cameraTrackControls.stop();
            cameraTrackControls.clear();
            if ($(".EarthFoot>div.active").find("i").hasClass("vis")) {
                $("#flyBotttom,#switch-box,#analysis-panel,.sceneVedio").show();
            } else {
                $("#flyBotttom,#switch-box,.sceneVedio").show();
            }
            $("#flyPogress").hide();
        });
        //放在标尺上显示 对应的图片 
        $(document).on("mouseover", ".flybottomTwo .progressBox>div", function () {
            var oThis = $(this);
            var PointId = oThis.attr("point-id");
            var currentPointId = oThis.attr("img-url");

            var progressHand = $(".flybottomTwo .hand");
            progressHand.show();
            var progressHand = $(".flybottomTwo .hand");
            progressHand.find("img").eq(0).attr("src", getRootPath() + "/" + currentPointId);
            $(".flybottomTwo .hand p").html("视点" + PointId);
        });
        //鼠标滑动时图片跟着滑动 
        $(document).on("mousemove", ".flybottomTwo .progressBox", function (e) {
            //元素离左边框宽度 
            var oThis = $(this).offset().left;
            //鼠标离左边框的宽度 
            var xx = e.originalEvent.x || e.originalEvent.layerX || 0;
            var oleft = -36 + (xx - oThis);
            $(".flybottomTwo .hand").css({ "left": oleft + "px" });
        });
        //离开标尺图片消失 
        $(document).on("mouseleave", ".flybottomTwo .progressBox>div", function () {
            var progressHand = $(".flybottomTwo .hand");
            progressHand.hide();
        });

    }
}